package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.common.R;
import com.itheima.dao.CategoryMapper;
import com.itheima.dao.DishMapper;
import com.itheima.dao.SetmealMapper;
import com.itheima.domain.Category;
import com.itheima.domain.Dish;
import com.itheima.domain.Setmeal;
import com.itheima.dto.DishDto;
import com.itheima.service.CategoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public void remove(Long id) {
        //检查当前分类下是否有对应的菜品（根据id查询）
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, id);
        Integer dishcount = dishMapper.selectCount(wrapper);
        if (dishcount > 0) {
            //存在，不能删除
            //  业务异常，抛出
            throw new CustomException("这个分类下有菜品，不能删除");
        }


        //检查单签分类下是否有对应的套餐（根据id查询）
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId, id);
        Integer setmealcount = setmealMapper.selectCount(queryWrapper);
        if (setmealcount > 0) {
            //存在不能删除
            //  业务异常，抛出
            throw new CustomException("这个分类下有套餐，不能删除");

        }

        //不存在，可以删除
        categoryMapper.deleteById(id);

    }




}
